<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>密码框显现</title>
</head>
<style>
    *{
        margin: 0;
        padding: 0;
    }
    :focus{
        outline: none;
    }
    .box{
        width: 250px;
        position: relative;
    }
    input{
        padding: 10px;
    }
    img{
        position: absolute;
        right: 30px;
        top: 50%;
        transform: translateY(-50%);
        cursor: pointer;
    }
</style>
<body>
    <div class="box">
        <label for="pws">密码框</label>
        <input type="password" id="pws">
        <img src="images/close.png" alt="">
    </div>

</body>
<script>
    var pws = document.getElementById('pws')
    var img = document.querySelector('img')


    //  1.
    // var flag = 1;
    // img.onclick = function(){
    //     if(flag){
    //         this.src = 'images/open.png'
    //         pws.type = 'text'
    //         flag = 0;
    //     }else{
    //         this.src = 'images/close.png'
    //         pws.type = 'password'
    //         flag = 1;
    //     }
    // }

    
    // console.log(url);
    // img.onclick = function(){
    //     var url = this.src
    //     if(url.indexOf('close') != -1){
    //         this.src = 'images/open.png'
    //         pws.type = 'text'
    //         console.log(url);
    //     }else{
    //         this.src = 'images/close.png'
    //         pws.type = 'password'
    //     }
    // }

    //
    var url = img.src
    img.onclick = function(){
        if(url == this.src){
            this.src = 'images/open.png'
            pws.type = 'text'
            console.log(url);
        }else{
            this.src = 'images/close.png'
            pws.type = 'password'
        }
    }
</script>

</html>